;(function () {
    //
    // 页面刷新时执行
    window.onload = function() {
        $("#nav_vm_list").addClass("active");
    };

    // 校验虚拟机参数
    function valid_vm_edit_bandwidth_data(obj){

        if(!(obj.bandwidth)){
            alert(gettext('必须填写带宽信息'));
            return false;
        }
        return true;
    }

    // 修改虚拟机表单提交按钮点击事件
    $('form#id-form-vm-edit-bandwidth button[type="submit"]').click(function (e) {
        e.preventDefault();

        let form = $('form#id-form-vm-edit-bandwidth');
        let obj_data = getForm2Obj(form);
        if (!valid_vm_edit_bandwidth_data(obj_data)){
            return;
        }
        if(!confirm(gettext('确定修改虚拟机？')))
            return;

        let vm_uuid = $('#id-edit-vm-uuid').text();
        let bandwidth = obj_data['bandwidth']
        let mac_ip = obj_data['mac_ip']
        let api = build_absolute_url('api/v3/vms/'+ vm_uuid + '/bandwidth/' );
        if (bandwidth){
            api = api + '?bandwidth=' + bandwidth
        }else {
            alert(gettext('必须填写带宽信息'));
            return false;
        }

        if ( mac_ip && (mac_ip !== '0')){
            api = api + '&attach_ip=' + mac_ip
        }
        let btn_submit = $(this);
        btn_submit.addClass('disabled'); //鼠标悬停时，使按钮表现为不可点击状态
        btn_submit.attr('disabled', true);//失能对应按钮
        $.ajax({
            url: api,
            type: 'post',
            contentType: 'application/json',
            success: function (data, status, xhr) {
                if (xhr.status === 200){
                    alert(gettext('修改成功'));
                    location.reload();
                }else{
                    alert(gettext("修改失败！") + data.code_text);
                }
            },
            error: function (xhr) {
                let msg = gettext('修改带宽信息失败!');
                try{
                    msg = msg + xhr.responseJSON.code_text;
                }catch (e) {}
                alert(msg);
            },
            complete: function () {
                btn_submit.removeClass('disabled');   //鼠标悬停时，使按钮表现为可点击状态
                btn_submit.attr('disabled', false); //激活对应按钮
            }
        })
    });

})();